Cpanel-iContact-Provider-Local.t 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. use strict;
  2. use warnings;
  3. use Cwd qw{abs_path};
  4. use File::Basename qw{dirname};
  5. use lib abs_path( dirname(__FILE__) . "/../lib" );
  6. use Test::More;
  7. use Test::Fatal;
  8. use Test::Deep;
  9. use File::Temp ();
  10. use Cpanel::iContact::Provider::Local ();
  11. use Cpanel::iContact::Provider::Local::Getter ();
  12. plan tests => 1;
  13. # First, let's mock out the parent, and other stuff we wouldn't wanna do in a unit test
  14. subtest "Provider bits work as expected ('unit' test)" => sub {
  15. plan tests => 7;
  16. # Create tempdir for jamming stuff into
  17. my $tmp_obj = File::Temp->newdir();
  18. my $tmp_dir = $tmp_obj->dirname;
  19. $Cpanel::iContact::Provider::Local::DIR = "$tmp_dir/iContact_notices";
  20. # Make the notice send
  21. isa_ok( my $spammer = Cpanel::iContact::Provider::Local->new(), "Cpanel::iContact::Provider::Local" );
  22. my $ex = exception { $spammer->send() };
  23. is( $ex, undef, "send doesn't throw on GreatSuccess" ) || diag explain $ex;
  24. my $user = getpwuid($<);
  25. my @files = glob( "$tmp_dir/iContact_notices/$user/*.json" );
  26. ok( scalar(@files), "Looks like a file was written..." ) || diag explain \@files;
  27. like( $files[0], qr/\d\.json/, "..and it looks like we'd expect it to" ) || diag explain \@files;
  28. # Now let's check on them another way
  29. my %notifications = Cpanel::iContact::Provider::Local::Getter::get_all_notices( 'user' => $user );
  30. ok( scalar(keys(%notifications)), "Got the expected notifications from hash..." ) || diag explain \%notifications;
  31. my $hash_key = (keys(%notifications))[0];
  32. like( $hash_key, qr/^\d+$/, "..and the hash key looks like we'd expect it to" ) || diag explain \%notifications;
  33. my $model = {
  34. $hash_key => {
  35. 'subject' => 'cPanel on “Drugs”',
  36. 'text' => 'HOLY CRAP THE “AUTO-LAYOFF” THING TRIGGERED',
  37. 'html' => '<p>HOLY CRAP THE “AUTO-LAYOFF” THING TRIGGERED</p>',
  38. }
  39. };
  40. cmp_deeply( \%notifications, $model, "%notifications hashref has the expected return" ) || diag explain \%notifications;
  41. };